import base64
import hmac

from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

from myapp.models import User,Goods,Cate
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

from qiniu import Auth
#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid
from hashlib import sha256
import urllib

from myapp.myser import *


class GoodsView(APIView):

    def post(self,request):
        name = request.POST.get('name')
        desc = request.POST.get('desc')
        img = request.POST.get('img')
        cid = request.POST.get('cid')
        video = request.POST.get('video')
        price = request.POST.get('price')
        param = request.POST.get('param')

        goods = Goods.objects.filter(name=name).first()

        if goods:
            res = {}
            res['code'] = 400
            res['message'] = '商品已存在'
            return Response(res)

        g = Goods(name=name,desc=desc,img=img,cid=cid,video=video,price=price,param=param)
        g.save()

        res = {}
        res['code'] = 200
        res['message'] = '商品已保存'

        return Response(res)


class CateView(APIView):

    def post(self,request):

        cname = request.POST.get('cname')

        c = Cate(cname=cname)
        c.save()
        res = {}
        res['code'] = 200
        res['message'] = '分类保存成功'

        return Response(res)
